package cn.sdut.training.web.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

import cn.sdut.training.domain.Record;
import cn.sdut.training.domain.Tag;
import cn.sdut.training.domain.User;
import cn.sdut.training.domain.UserAccount;
import cn.sdut.training.service.RecordService;
import cn.sdut.training.service.TrainingService;
import cn.sdut.training.service.UserService;
import cn.sdut.training.utils.HqlHelper;
import cn.sdut.training.utils.PageView;

public class RecordAction extends ActionSupport implements SessionAware{

	private static final long serialVersionUID = 4112525415555761984L;
	
	private Record record;
	private User user;
	private List<UserAccount> accountList;
	private List<Tag> tagList;
	private List<Record> recordList;

	private Map<String, Object> session;
	private Long id;			//recorId,用于代码的删除和更新
	private Long userAccountId;	//用户账号Id，用于代码的保存和更新
	private Long tagId;			//标签Id，用于代码的保存和更新
	
	private PageView pageView;	//分页信息
	private Integer pageNum = 1;//要查看的页面
	
	@Resource
	private RecordService recordService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private TrainingService trainingService;

	//------------------------------------------------------
	
	/**
	 * 转到添加代码页面 
	 */
	public String toSavePage(){
		//初始化用户账号信息
		user = (User) session.get("user");
		if(user!=null){
			accountList = userService.getUserAccountListByUserId(user.getId());
		}
		//初始化标签列表
		tagList = trainingService.findAllTags();
		
		return "toSavePage";
	}
	
	/**
	 * 处理添加页面操作 
	 */
	public String save(){
		UserAccount account = userService.getUserAccountById(userAccountId);
		record.setUserAccount(account);
		User user = (User) session.get("user");
		record.setUser(user);
		recordService.save(record);
		return "toList";
	}
	
	/**
	 * 删除记录
	 */
	public String delete(){
		recordService.delete(id);
		return "toList";
	}
	
	/**
	 * 转向编辑页面
	 */
	public String toEditPage(){
		record = recordService.getById(id);
		return "toEditPage";
	}
	
	/**
	 * 执行更新操作..
	 */
	public String edit(){
		recordService.update(record);
		return "toList";
	}
	
	/**
	 * 转向代码列表页面
	 */
	public String toListPage(){
		User user = (User) session.get("user");
		if(user!=null){
			// a, 构建查询条件
			HqlHelper hqlHelper = new HqlHelper(Record.class)//
					.addWhereCondition("user.id=?", user.getId())//
					.addOrderProperty("id", false);//
			// b, 查询分页信息
			pageView = recordService.getPageView(hqlHelper, pageNum);
		}
		return "toListPage";
	}
	
	//----------------------------------------------
	public Record getRecord() {
		return record;
	}

	public void setRecord(Record record) {
		this.record = record;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public List<UserAccount> getAccountList() {
		return accountList;
	}

	public void setAccountList(List<UserAccount> accountList) {
		this.accountList = accountList;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getUserAccountId() {
		return userAccountId;
	}

	public void setUserAccountId(Long userAccountId) {
		this.userAccountId = userAccountId;
	}

	public List<Tag> getTagList() {
		return tagList;
	}

	public void setTagList(List<Tag> tagList) {
		this.tagList = tagList;
	}

	public List<Record> getRecordList() {
		return recordList;
	}

	public void setRecordList(List<Record> recordList) {
		this.recordList = recordList;
	}

	public Long getTagId() {
		return tagId;
	}

	public void setTagId(Long tagId) {
		this.tagId = tagId;
	}

	public PageView getPageView() {
		return pageView;
	}

	public void setPageView(PageView pageView) {
		this.pageView = pageView;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}
	
	
}
